﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Critex.Models;

namespace Critex.Helpers
{
    public static class ContentHelper
    {

        private static Section _currentSection;

        public static ContentArea GetContentArea(this HtmlHelper helper, string title)
        {
            return _currentSection.ContentAreas.First(c => c.Title == title);
        }

        public static void WriteContentAreaEditField(this HtmlHelper helper, string title, int sectionId)
        {
            var contentArea = GetContentArea(helper, title);
            WriteContentAreaEditField(helper, contentArea, sectionId);
        }

        public static IEnumerable<ImageArea> GetImageCollectionAreaContents(this HtmlHelper helper, string title)
        {
            var area = (ImageCollectionArea)GetContentArea(helper, title);
            return GetImageCollectionAreaContents(helper, area);
        } 

        public static IEnumerable<ImageArea> GetImageCollectionAreaContents(this HtmlHelper helper, ImageCollectionArea area)
        {
            return area.ImageAreas;
        }

        public static string WriteContentAreaEditField(this HtmlHelper helper, ContentArea contentArea, int sectionId)
        {
            return contentArea.WriteEditField(helper, sectionId);
        }

        public static string WriteAddressAreaAddress(this HtmlHelper helper, string title)
        {
            var area = (AddressArea)GetContentArea(helper, title);
            return WriteAddressAreaAddress(helper, area);
        }

        public static string WriteAddressAreaAddress(this HtmlHelper helper, AddressArea area)
        {
            return area.Address;
        }

        public static IHtmlString WriteHtmlAreaValue(this HtmlHelper helper, string title)
        {
            var area = (HtmlArea)GetContentArea(helper, title);
            return WriteHtmlAreaValue(helper, area);
        }

        public static IHtmlString WriteHtmlAreaValue(this HtmlHelper helper, HtmlArea area)
        {
            return area.WriteValue(helper);
        }

        public static IDisposable UseRootSection(this HtmlHelper helper, string title)
        {
            return UseSection(helper, title, true);
        }

        public static IDisposable UseSection(this HtmlHelper helper, string title, bool useRoot = false)
        {
            var container = ConnectionHelper.GetContainer();
            {
                if (_currentSection != null && !useRoot)
                {
                    return UseSection(helper, _currentSection.SubSections.First(s => s.Title == title));
                } else
                {
                    return UseSection(helper, container.Sections.First(s => s.Title == title));
                }
            }
        }

        public static IDisposable UseSection(this HtmlHelper helper, Section section)
        {
            _currentSection = section;
            return null;
        }

    }
}